package com.example.controller;

import com.example.entity.authority.Party;
import com.example.ext.entity.ExtParty;
import com.example.service.PartyService;
import com.example.util.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by youkun on 2017/9/9.
 */
@RestController
public class PartyController {

    @Autowired
    private PartyService parytService;

    @ApiOperation(value = "查询机构列表的信息",notes = "获取机构列表信息")
    @RequestMapping(value="api/party/queryList",method = RequestMethod.POST)
    Map<String,Object> queryList(@RequestBody ExtParty extParty) {

        return parytService.queryList(extParty);
    }

    @ApiOperation(value = "查询双随机机构列表的信息",notes = "查询双随机机构列表的信息")
    @RequestMapping(value="api/party/queryRandomList",method = RequestMethod.POST)
    Map<String,Object> queryRandomList(@RequestBody Map extParty) {

        return parytService.queryRandomList(extParty);
    }

    @ApiOperation(value = "查询功能列表的信息",notes = "获取功能列表信息")
    @RequestMapping(value="register/party/queryList",method = RequestMethod.POST)
    Map<String,Object> registerQueryList(@RequestBody ExtParty extParty) {

        return parytService.queryList(extParty);
    }

    @ApiOperation(value = "查询指定机构下的机构列表",notes = "查询指定机构下的机构列表")
    @RequestMapping(value="api/party/queryByIdList/{id}",method = RequestMethod.POST)
    Map<String,Object> queryByIdList(@PathVariable String id) {

        return parytService.queryByIdList(id);
    }

    @ApiOperation(value = "添加功能的信息",notes = "添加功能的信息")
    @RequestMapping(value="api/party/add",method = RequestMethod.POST)
    Map<String,Object> add(@RequestBody Party party) {

        return parytService.add(party);
    }

    @ApiOperation(value = "修改功能的信息",notes = "修改功能的信息")
    @RequestMapping(value="api/party/update",method = RequestMethod.POST)
    Map<String,Object> update(@RequestBody Party party) {

        return parytService.update(party);
    }


    @ApiOperation(value = "查询功能详细信息",notes = "查询功能详细信息")
    @RequestMapping(value="api/party/queryById/{id}",method = RequestMethod.POST)
    Map<String,Object> queryById(@PathVariable(value = "id") String id) {

        return parytService.queryById(id);
    }

    @ApiOperation(value = "查询功能详细信息",notes = "查询功能详细信息")
    @RequestMapping(value="api/party/queryByNameLk",method = RequestMethod.POST)
    Map<String,Object> queryByNameLk(@RequestBody Map map) {
        return parytService.queryByNameLk(map);
    }

    @ApiOperation(value = "删除功能信息",notes = "删除功能详细信息")
    @RequestMapping(value="api/party/deleteById/{id}",method = RequestMethod.POST)
    Map<String,Object> deleteById(@PathVariable(value = "id") String id) {

        return parytService.deleteById(id);
    }

    @ApiOperation(value = "批量删除功能信息",notes = "批量删除功能详细信息")
    @RequestMapping(value="api/party/deleteListById",method = RequestMethod.POST)
    Map<String,Object> deleteListById(@RequestBody Map<String, Object> inMap) {
        List<Map<String,Object>> resList = new ArrayList<>();
        Map<String,Object> returnMap = ResultUtil.returnSuccess("成功",null);
        List ids = (List)inMap.get("ids");

        //循环删除
        for (int i=0; i<ids.size(); i++) {
            resList.add(parytService.deleteById(ids.get(i)+""));
        }
        //判断结果集
        for (int i = 0; i < resList.size() ; i++) {
            if((resList.get(i).get("status")+"").equals("200")){
                break;
            }else{
                returnMap = ResultUtil.returnError("失败",null);
            }

        }
        return returnMap;

    }


    @ApiOperation(value = "查询父机构下某个类型的机构列表",notes = "查询父机构下某个类型的机构列表")
    @RequestMapping(value="api/party/queryByIdAndTypeList",method = RequestMethod.POST)
    Map<String,Object> queryByIdAndTypeList(@RequestBody Party party) {

        return parytService.queryByIdAndTypeList(party);
    }

    @ApiOperation(value = "导入机构列表",notes = "导入机构列表")
    @RequestMapping(value="party/importExcelParty",method = RequestMethod.POST)
    Map<String,Object> importExcelParty(@RequestBody MultipartFile file,@RequestParam("partyId") String parentId){

        return parytService.importExcelParty(file,parentId);
    }

    @ApiOperation(value = "查询机构的信息是否完善",notes = "查询机构的信息是否完善")
    @RequestMapping(value="api/party/checkParytIdMessage/{id}",method = RequestMethod.POST)
    Map<String,Object> checkParytIdMessage(@PathVariable String id) {

        return parytService.checkParytIdMessage(id);
    }

    @RequestMapping(value="api/party/queryPartyByPartyLevel",method = RequestMethod.POST)
    Map<String,Object> queryPartyByPartyLevel(@RequestBody Map map){
        return parytService.queryPartyByPartyLevel(map);
    }
}
